<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
	<title>天天生鲜-商品详情</title>
	<link rel="stylesheet" type="text/css" href="/static/css/reset.css">
	<link rel="stylesheet" type="text/css" href="/static/css/main.css">
</head>
<body>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到天天生鲜!</div>
			<div class="fr">
				{% if user.is_authenticated %}
				<div class="login_btn fl">
					欢迎您：<em>{{user.username}}</em>
					<a href="{% url 'user:logout' %}">登出</a>
				</div>
				{% else %}
				<div class="login_btn fl">
					<a href="{% url 'user:login' %}">登录</a>
					<span>|</span>
					<a href="{% url 'user:register' %}">注册</a>
				</div>
				{% endif %}
				<div class="user_link fl">
					<span>|</span>
					<a href="{% url 'user:user' %}">用户中心</a>
					<span>|</span>
					<a href="{% url 'cart:index' %}">我的购物车</a>
					<span>|</span>
					<a href="{% url 'user:order' 1 %}">我的订单</a>
				</div>
			</div>
		</div>		
	</div>

	<div class="search_bar clearfix">
		<a href="{% url 'goods:index' %}" class="logo fl"><img src="/static/images/logo.png"></a>
		<div class="search_con fl">
			<form method="get" action="/search">
				<input type="text" class="input_text fl" name="q" placeholder="搜索商品">
				<input type="submit" class="input_btn fr" name="" value="搜索">
			</form>
		</div>
		<div class="guest_cart fr">
			<a href="{% url 'cart:index' %}" class="cart_name fl">我的购物车</a>
			<div class="goods_count fl" id="show_count">{{cart_num}}</div>
		</div>
	</div>

	<div class="navbar_con">
		<div class="navbar clearfix">
			<div class="subnav_con fl">
				<h1>全部商品分类</h1>	
				<span></span>			
				<ul class="subnav">
					{% for type in types %}
					<li><a href="#" class="{{type.logo}}">{{type.name}}</a></li>
					{% endfor %}
				</ul>
			</div>
			<ul class="navlist fl">
				<li><a href="{% url 'goods:index' %}">首页</a></li>
				<li class="interval">|</li>
				<li><a href="{% url 'goods:index' %}">手机生鲜</a></li>
				<li class="interval">|</li>
				<li><a href="{% url 'goods:index' %}">抽奖</a></li>
			</ul>
		</div>
	</div>

	<div class="breadcrumb">
		<a href="#">全部分类</a>
		<span>></span>
		<a href="#">新鲜水果</a>
		<span>></span>
		<a href="#">商品详情</a>
	</div>

	<div class="goods_detail_con clearfix">
		<div class="goods_detail_pic fl"><img src="{{sku.image.url}}"></div>

		<div class="goods_detail_list fr">
			<form method="post" action="{% url 'order:place' %}" id="detail">
				<input type="hidden" name="sku_ids" value="{{sku.id}}">
			<h3>{{sku.name}}</h3>
			{% csrf_token %}
			<p>{{sku.desc}}</p>
			<p>其他规格</p>
			<ul>
				{% for same_sku in same_skus %}
				<li><a href="{% url 'goods:detail' same_sku.id %}">{{same_sku.name}}</a></li>
				{% empty %}
				<p>无其他规格</p>
				{% endfor %}
			</ul>
			<div class="prize_bar">
				<span class="show_pirze">¥<em>{{sku.price}}</em></span>
				<span class="show_unit">{{sku.unite}}</span>
			</div>
			<div class="goods_num clearfix">
				<div class="num_name fl">数 量：</div>
				<div class="num_add fl">
					<input type="text" class="num_show fl" value="1">
					<a href="javascript:;" class="add fr">+</a>
					<a href="javascript:;" class="minus fr">-</a>	
				</div> 
			</div>
			<div class="total">总价：<em>16.80元</em></div>
			<div class="operate_btn">
				<a href="javascript:;" onclick="$('#detail').submit();" class="buy_btn">立即购买</a>
				<a href="javascript:;" class="add_cart" id="add_cart" sku_id="{{sku.id}}">加入购物车</a>
			</div>
			</form>
		</div>
	</div>

	<div class="main_wrap clearfix">
		<div class="l_wrap fl clearfix">
			<div class="new_goods">
				<h3>新品推荐</h3>
				<ul>
					{% for new in news %}
					<li>
						<a href="{% url 'goods:detail' new.id %}"><img src="{{new.image.url}}"></a>
						<h4><a href="{% url 'goods:detail' new.id %}">{{new.name}}</a></h4>
						<div class="prize">{{new.price}}</div>
					</li>
					{% endfor %}
				</ul>
			</div>
		</div>

		<div class="r_wrap fr clearfix">
			<ul class="detail_tab clearfix">
				<li class="active tag_detail">商品介绍</li>
				<li class="tag_comment">评论</li>
			</ul>

			<div class="tab_content" id="tab_detail">
				<dl>
					<dt>商品详情：</dt>
					<dd>{{goods.detail|default:"无详情信息"}}</dd>
				</dl>
			</div>

            <div class="tab_content" id="tab_comment" style="display: none">
				<dl>
                    {% for order in sku_orders %}
					<dt>评论时间：{{ order.update_time }}&nbsp;&nbsp;用户名:{{ order.order.user.username }}</dt>
                    <dd>评论内容:{{ order.comment }}</dd>
                    {% endfor %}
				</dl>
			</div>
		</div>
	</div>

	<div class="footer">
		<div class="foot_link">
			<a href="#">关于我们</a>
			<span>|</span>
			<a href="#">联系我们</a>
			<span>|</span>
			<a href="#">招聘人才</a>
			<span>|</span>
			<a href="#">友情链接</a>		
		</div>
		<p>电话：010-****888    蜀ICP备18018826号</p>
		<p>工信部备案网址：<a href="http://www.miitbeian.gov.cn/">http://www.miitbeian.gov.cn/</a></p>
	</div>
	<div class="add_jump"></div>

	<script type="text/javascript" src="/static/js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript">
		$(".buy_btn").click(function(e){

		});

	    $(".tag_detail").click(function(e){
	        $(this).addClass("active");
	        $(".tag_comment").removeClass("active");
	        $("#tab_detail").css("display","block");
	        $("#tab_comment").css("display","none");
	    });

	    $(".tag_comment").click(function(e){
	        $(".tag_detail").removeClass("active");
	        $(this).addClass("active");
	        $("#tab_detail").css("display","none");
	        $("#tab_comment").css("display","block")
	    });

		update_total();
		function update_total(){
			var num=$(".num_show").val();
			var price=$(".show_pirze em").text();
			price=parseFloat(price);
			var total=num*price;
			$(".total em").text(total.toFixed(2)+"￥");
		}
		$(".add").click(function(e){
			var num=$(".num_show").val();
			num=parseInt(num);
			num++;
			$(".num_show").val(num);
			update_total();
		});

		$(".minus").click(function(e){
			var num=$(".num_show").val();
			num=parseInt(num);
			num--;
			if(num<=1){
				num=1;
			}
			$(".num_show").val(num);
			update_total();
		});

		$(".num_show").blur(function(e){
			// 获取用户输入的数目
            count = $(this).val()
            // 校验count是否合法
            if (isNaN(count) || count.trim().length==0 || parseInt(count) <=0){
                count = 1
            }
            // 重新设置商品的数目
            $(this).val(parseInt(count))

			update_total();
		});

		var $add_x = $('#add_cart').offset().top;
		var $add_y = $('#add_cart').offset().left;

		var $to_x = $('#show_count').offset().top;
		var $to_y = $('#show_count').offset().left;

		$("#add_cart").click(function(e){
			sku_id=$(this).attr("sku_id");
			count=$(".num_show").val();
			token=$("input[name='csrfmiddlewaretoken']").val()

			params={
				"sku_id":sku_id,
				"count":count,
				"csrfmiddlewaretoken":token
			}

			$.post("/cart/add",params,function(res){
				if (res.res==5){
					//添加成功
					$(".add_jump").css({'left':$add_y+80,'top':$add_x+10,'display':'block'});
					$(".add_jump").stop().animate({
						'left': $to_y+7,
						'top': $to_x+7},
						"fast", function() {
							$(".add_jump").fadeOut('fast',function(){
								$('#show_count').html(res.total_count);
							});

					});

				}else{
					alert(res.errmsg);
				}

			},"json");


		});



	</script>
	
</body>
</html>